package com.edu.util;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class JacksonUtil {
    private static Logger logger = LoggerFactory.getLogger(JacksonUtil.class);

    private JacksonUtil() {
    }

    public static ObjectMapper mapper() {
        return mapper("yyyy-MM-dd HH:mm:ss");
    }

    public static ObjectMapper mapper(String dateFormat) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        SimpleDateFormat myDateFormat = new SimpleDateFormat(dateFormat);
        mapper.setDateFormat(myDateFormat);
        mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return mapper;
    }

    public static boolean isEmpty(Object o) {
        return BeanUtil.isEmpty(o);
    }

    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    public static boolean isJson(String jsonStr) {
        return isJsonObject(jsonStr) || isJsonArray(jsonStr);
    }

    public static void assert0(String jsonStr, String message) {
        boolean isJson = isJsonObject(jsonStr) || isJsonArray(jsonStr);
        if (!isJson) {
            throw new IllegalArgumentException(message);
        }
    }

    public static boolean isNotJson(String jsonStr) {
        return !isJson(jsonStr);
    }

    public static boolean isJsonObject(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return false;
        } else {
            try {
                JsonNode jsonNode = mapper().readTree(jsonStr);
                return jsonNode.isObject();
            } catch (Exception var2) {
                logger.warn(var2.getMessage(), var2);
                return false;
            }
        }
    }

    public static boolean isNotJsonObject(String jsonStr) {
        return !isJsonObject(jsonStr);
    }

    public static boolean isJsonArray(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return false;
        } else {
            try {
                JsonNode jsonNode = mapper().readTree(jsonStr);
                return jsonNode.isArray();
            } catch (Exception var2) {
                logger.warn(var2.getMessage(), var2);
                return false;
            }
        }
    }

    public static boolean isNotJsonArray(String jsonStr) {
        return !isJsonArray(jsonStr);
    }

    public static String toJsonString(Object obj) {
        if (BeanUtil.isEmpty(obj)) {
            return null;
        } else {
            try {
                return mapper().writeValueAsString(obj);
            } catch (JsonProcessingException var2) {
                logger.warn(var2.getMessage(), var2);
                return null;
            }
        }
    }

    public static String getString(String data, String key) {
        Map<String, Object> dataMap = (Map)getDTO(data, Map.class);
        Object d = dataMap.get(key);
        return d instanceof String ? d.toString() : toJsonString(dataMap.get(key));
    }

    public static Object get(String data, String key) {
        Map<String, Object> dataMap = (Map)getDTO(data, Map.class);
        return dataMap.get(key);
    }

    public static Map<String, Object> toMap(String data) {
        return (Map)getDTO(data, Map.class);
    }

    public static <T> T getDTO(String jsonStr, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        } else {
            try {
                ObjectMapper mapper = mapper();
                return mapper.readValue(jsonStr, clazz);
            } catch (IOException var3) {
                logger.warn(var3.getMessage(), var3);
                return null;
            }
        }
    }

    public static <T> T getDTO(String jsonStr, Class<T> clazz, String dateFormat) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        } else {
            try {
                ObjectMapper mapper = mapper(dateFormat);
                return mapper.readValue(jsonStr, clazz);
            } catch (IOException var4) {
                logger.warn(var4.getMessage(), var4);
                return null;
            }
        }
    }

    public static <T> List<T> getDTOList(String jsonStr, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        } else {
            try {
                ObjectMapper mapper = mapper();
                JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
                return (List)mapper.readValue(jsonStr, type);
            } catch (IOException var4) {
                logger.warn(var4.getMessage(), var4);
                return null;
            }
        }
    }

    public static <T> List<T> getDTOList(String jsonStr, Class<T> clazz, String dateFormat) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        } else {
            try {
                ObjectMapper mapper = mapper(dateFormat);
                JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
                return (List)mapper.readValue(jsonStr, type);
            } catch (IOException var5) {
                logger.warn(var5.getMessage(), var5);
                return null;
            }
        }
    }
}
